package cn.xxc.org.controller;

import cn.xxc.basic.util.AjaxResult;
import cn.xxc.basic.util.PageList;
import cn.xxc.org.domain.Department;
import cn.xxc.org.query.DepartmentQuery;
import cn.xxc.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @GetMapping
    public List<Department> findAll(){
        return departmentService.loadAll();
    }

    @GetMapping("/{id}")
    public Department findOne(@PathVariable("id") Long id ){
        return departmentService.loadByid(id);
    }

    @GetMapping("/getTree")
    public List<Department> getTree(){
        return departmentService.getTree();
    }

    @PutMapping
    public AjaxResult save(@RequestBody Department department){
        try {
            if (department.getId()==null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult(false, "系统繁忙，请重试！！");
        }
    }

    @DeleteMapping("/{id}")
    public AjaxResult del(@PathVariable("id")Long id){
        try {
            departmentService.del(id);
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult(false, "系统繁忙，请重试！！");
        }
    }

    @PostMapping
    public PageList<Department> queryList(@RequestBody DepartmentQuery departmentQuery){
        try {
            Integer totals=departmentService.queryCount(departmentQuery);
            List<Department> data=departmentService.queryData(departmentQuery);
            System.out.println(data);
            return new PageList<>(totals,data);
        }catch (Exception e){
            return new PageList<>();
        }
    }

    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            departmentService.patchDel(ids);
            return new AjaxResult();
        }catch (Exception e){
            return new AjaxResult(false, "系统繁忙，请重试！！");
        }
    }
}
